{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualization of Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The goal of this experiment is to study the goal of making data more visual, more easily understandable, in an intuitive manner." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Table of Contents\n", "* [Visualization of Data](#Visualization-of-Data)\n", "\t* [Points](#Points)\n", "\t* [Lines](#Lines)\n", "\t* [Bar charts](#Bar-charts)\n", "\t* [Pie charts](#Pie-charts)\n", "\t* [Geographic: Maps + data](#Geographic:-Maps-+-data)\n", "\t\t* [PShape](#PShape)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Points\n", "\n", "A plot made up of just points of data is sometimes called a \"scatter plot\" because the data are often scattered all over the graph." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #1:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #1 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "int border;\n", "\n", "void setup() {\n", " size(500, 200); \n", " border = 20;\n", "}\n", "\n", "void drawAxises(int x, int y, int w, int h) {\n", " fill(255);\n", " rect(x, y, w, h);\n", " line(x + border, y + border, x + border, y + h - border);\n", " line(x + border, y + h - border, y + w - border, y + h - border);\n", "}\n", "\n", "void drawPoint(int x, int y, int w, int h, float px, float py) {\n", " fill(255, 0, 0);\n", " ellipse(px + border + x, h + y - border - py, 5, 5);\n", "}\n", "\n", "void draw() {\n", " int chartX = 20;\n", " int chartY = 10;\n", " int chartWidth = width - chartX * 2;\n", " int chartHeight = height - chartY * 2;\n", " drawAxises(chartX, chartY, chartWidth, chartHeight);\n", " \n", " for (int i = 0; i < chartWidth - border * 2; i += 10) {\n", " drawPoint(chartX, chartY, chartWidth, chartHeight, i, random(chartHeight - border * 2));\n", " }\n", " noLoop();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lines" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make a Line plot, we only need keep a running place holder for the last point draw, and connect to the current point." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #2:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #2 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "int border;\n", "int prev_x = -1, prev_y = -1;\n", "\n", "void setup() {\n", " size(500, 200); \n", " border = 20;\n", "}\n", "\n", "void drawAxises(int x, int y, int w, int h) {\n", " fill(255);\n", " rect(x, y, w, h);\n", " line(x + border, y + border, x + border, y + h - border);\n", " line(x + border, y + h - border, y + w - border, y + h - border);\n", "}\n", "\n", "void drawPoint(int x, int y, int w, int h, float px, float py) {\n", " fill(255, 0, 0);\n", " ellipse(px + border + x, h + y - border - py, 5, 5);\n", " if (prev_x != -1) {\n", " line(px + border + x, h + y - border - py, prev_x, prev_y);\n", " }\n", " prev_x = px + border + x;\n", " prev_y = h + y - border - py;\n", "}\n", "\n", "void draw() {\n", " int chartX = 20;\n", " int chartY = 10;\n", " int chartWidth = width - chartX * 2;\n", " int chartHeight = height - chartY * 2;\n", " drawAxises(chartX, chartY, chartWidth, chartHeight);\n", " \n", " for (int i = 0; i < chartWidth - border * 2; i += 10) {\n", " drawPoint(chartX, chartY, chartWidth, chartHeight, i, random(chartHeight - border * 2));\n", " }\n", " noLoop();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bar charts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One can easily turn the points into height to draw a rectangle." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #29:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #29 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "int border;\n", "\n", "void setup() {\n", " size(500, 200); \n", " border = 20;\n", "}\n", "\n", "void clear(int x, int y, int w, int h) {\n", " fill(255);\n", " rect(x, y, w, h);\n", "}\n", "\n", "void drawAxises(int x, int y, int w, int h) {\n", " stroke(0);\n", " line(x + border, y + border, x + border, y + h - border);\n", " line(x + border, y + h - border, y + w - border, y + h - border);\n", "}\n", "\n", "\n", "void drawBar(int x, int y, int w, int h, int total, int pos, float percent, color c) {\n", " pw = (w - (border * 2)) / total;\n", " px = x + border + (pw * pos);\n", " ph = percent/100.0 * (h - border * 2);\n", " py = y + h - border - ph;\n", " fill(200);\n", " noStroke();\n", " rect(px + pw * .20 + 10, py + 10, pw - pw * .40, ph - 10);\n", " fill(c);\n", " stroke(c);\n", " rect(px + pw * .20, py, pw - pw * .40, ph);\n", "}\n", "\n", "void draw() {\n", " int chartX = 20;\n", " int chartY = 10;\n", " int chartWidth = width - chartX * 2;\n", " int chartHeight = height - chartY * 2;\n", " clear(chartX, chartY, chartWidth, chartHeight);\n", " \n", " drawBar(chartX, chartY, chartWidth, chartHeight, 5, 0, 100, color(255, 0, 0));\n", " drawBar(chartX, chartY, chartWidth, chartHeight, 5, 1, 50, color(0, 255, 0));\n", " drawBar(chartX, chartY, chartWidth, chartHeight, 5, 2, 25, color(0, 128, 128));\n", " drawBar(chartX, chartY, chartWidth, chartHeight, 5, 3, 75, color(0, 0, 255));\n", " drawBar(chartX, chartY, chartWidth, chartHeight, 5, 4, 10, color(128, 255, 128));\n", " drawAxises(chartX, chartY, chartWidth, chartHeight);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pie charts" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #40:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #40 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "int border;\n", "\n", "void setup() {\n", " size(200, 200); \n", " border = 20;\n", "}\n", "\n", "void drawBackground(int cx, int cy, int w, int h) {\n", " fill(255);\n", " ellipse(cx, cy, w, h);\n", "}\n", "\n", "void drawPie(int x, int y, int w, int h, float start, float percent, color c) {\n", " fill(c);\n", " rstart = start/100 * (2 * PI);\n", " rstop = rstart + percent/100 * (2 * PI);\n", " arc(x, y, w, h, rstart, rstop);\n", "}\n", "\n", "void draw() {\n", " int x = width/2;\n", " int y = height/2;\n", " int w = width - border * 2;\n", " int h = height - border * 2;\n", " //drawBackground(x, y, w, h);\n", " drawPie(x + 10, y + 10, w, h, 0, 25, color(255, 0, 0));\n", " drawPie(x - 10, y + 10, w, h, 25, 25, color(0, 255, 0));\n", " drawPie(x - 10, y - 10, w, h, 50, 10, color(0, 0, 255));\n", " drawPie(x + 10, y - 10, w, h, 60, 40, color(0, 255, 255));\n", "}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Geographic: Maps + data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we need to download a SVG (Scalable Vector Graphics) image file. We use the metacommand %download." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloaded '20091105194402%21Blank_US_Map.svg'.\n" ] } ], "source": [ "%download http://upload.wikimedia.org/wikipedia/commons/archive/3/32/20091105194402%21Blank_US_Map.svg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we rename the file using the \"shell\" command \"mv\" -- short for \"move\":" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "! mv 20091105194402%21Blank_US_Map.svg usa-wikipedia.svg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we can use the usa-wikipedia.svg file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### PShape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will use a shape object from Processing, called PShape. We can load a shape from an SVG image file.\n", "\n", "Likewise, we can get a part of the file by using the state abbreviations." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #5:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #5 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "PShape usa;\n", "PShape michigan;\n", "PShape ohio;\n", "\n", "void setup() {\n", " size(959, 593); \n", " usa = loadShape(\"usa-wikipedia.svg\");\n", " michigan = usa.getChild(\"MI\");\n", " ohio = usa.getChild(\"OH\");\n", "}\n", "\n", "void draw() {\n", " background(255);\n", " \n", " // Draw the full map\n", " shape(usa, 0, 0);\n", " \n", " // Disable the colors found in the SVG file\n", " michigan.disableStyle();\n", " // Set our own coloring\n", " fill(0, 51, 102);\n", " noStroke();\n", " // Draw a single state\n", " shape(michigan, 0, 0); // Wolverines!\n", " \n", " // Disable the colors found in the SVG file\n", " ohio.disableStyle();\n", " // Set our own coloring\n", " fill(153, 0, 0);\n", " noStroke();\n", " // Draw a single state\n", " shape(ohio, 0, 0); // Buckeyes!\n", " noLoop();\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "processing", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }